package com.ljy.jymail.seckill.controller;


import com.ljy.common.utils.R;
import com.ljy.jymail.seckill.service.SeckillService;
import com.ljy.jymail.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {


    @Autowired
    SeckillService seckillService;
    /*
    * 返回当前时间可以参与的秒杀商品
    * */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> result = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(result);
    }


    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){

        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }


    //处理秒杀请求
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                    @RequestParam("key") String key,
                    @RequestParam("num") Integer num, Model model
    ){
        //拦截器判断是否登录
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn",orderSn);

        return "success";
    }
}
